import { expect } from '@jest/globals';
import * as compressing from 'compressing';
import fsExtra from 'fs-extra';
import path from 'path';

import { buildAppAndCheckFile, commonJsonOperation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
  SignType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOption } from '../../plugins/task-builder/test-task-option';


const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.OpenHarmony,
    apiVersion: APIVersion.API10,
    apiType: APIType.FA,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.NativeC
  },
  modulesOption: {
    [ModuleTemplateType.EmptyAbility]: 1,
  }
};

const taskConfig: TestTaskOption = {
  caseLevel: CaseLevel.L1,
  sign: SignType.NoSign,
  ohpmInstall: IsEnable.DisAble,
};

const updateDevictType = async (fileType: string) => {
  await commonJsonOperation(path.resolve(fileType, 'src/main/config.json'), async (json): Promise<void> => {
    let data = json.module.deviceType;
    data = data.filter((item: string) => item !== 'phone' && item !== '2in1');
    json.module.deviceType = data;
  });
};

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const entryPath = project.getModule('entry').moduleRootPath();
  const applicationPath = project.getModule('application').moduleRootPath();
  updateDevictType(entryPath);
  updateDevictType(applicationPath);

  await buildAppAndCheckFile(project, taskConfig, record);
  const projectRootPath = project.projectRootPath();

  // 解析app包的内容
  await fsExtra.copyFile(path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned.app'),
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned.zip'));
  await compressing.zip.uncompress(
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned.zip'),
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned'));

  // 查看app包里是否有多个hap包
  const applicationHapExist = await fsExtra.pathExists(
    // eslint-disable-next-line max-len
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/application-entry-default.hap'));
  const entryHapExist = await fsExtra.pathExists(
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/entry-default.hap'));
  expect(applicationHapExist).toBe(true);
  expect(entryHapExist).toBe(true);
};

TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_Build_APP_FA_API10_ArkTs_003')
  .description('003、FA模型，ohos_C++多模块工程打APP包_entry和feature')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testFun)
  .load();
